// packageI/ecosphere_watercooler/owner/equipment/equipment.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    device_no: "",
    address: "",
    keyword: "",
    list: [],
    current_page:1,
    last_page:1,
    number: "",
    show: false,
    img_url:"",
    overlay_show:false,
  },
  onClickHide(){
      this.setData({overlay_show:false})
  },
  search_equipment(e){
    this.setData({keyword:e.detail})
  },
  onInput(e) {
    this.setData({
      address: e.detail.value
    })
  },
  handle(e){
    let {id} = e.currentTarget.dataset.item;
    app._getNetWork({
      url:app.getNetAddresss("plugin.water-machine.frontend.business.device.download-code"),
      data:{id},
      success:resdata=>{
        let {result,data,msg} = resdata.data;
        if(result){
          this.setData({
            img_url:data,
            overlay_show:true
          })
        }else{
          app.tips(msg);
        }
      }
    })
  },
  searchbtn(e) {
    this.setData({
      device_no: e.detail.value
    })
  },
  saveImg(){
    wx.downloadFile({
      url: this.data.img_url,
      success: function (res) {
        //图片保存到本地
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: function (data) {
            wx.showToast({
              title: '保存成功',
              icon: 'none',
              duration: 2000
            });
          },
          fail: function (err) {
            if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
              console.log("当初用户拒绝，再次发起授权");
              wx.openSetting({
                success(settingdata) {
                  if (settingdata.authSetting['scope.writePhotosAlbum']) {
                    wx.showToast({
                      icon: 'none',
                      title: '获取权限成功,再次长按保存图片',
                      duration: 1500
                    });
                  } else {
                    wx.showToast({
                      icon: 'none',
                      title: '获取权限失败,保存图片需要用户授权',
                      duration: 1500
                    });
                  }
                }
              });
            }
          },
          complete(res) {
            console.log(res);
          }
        });
      }
    });
  },
  submit() {
    app._postNetWork({
      url: app.getNetAddresss('plugin.water-machine.frontend.business.device.store'),
      data: {
        device_no: this.data.device_no,
        address: this.data.address
      },
      success: resdata => {
        let {
          result,
          msg
        } = resdata.data;
        if (result) {
          this.setData({
            show: false,
            device_no: "",
            address: ""
          })
          this.getData()
        }
        app.tips(msg)
      }

    })
  },
  onChange(e) {
    console.log(e);
  },
  onClose() {
    this.setData({
      show: false
    })
  },
  onShowPopup() {
    this.setData({
      show: true
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData()
  },
  getData(page = 1) {
    app._getNetWork({
      url: app.getNetAddresss('plugin.water-machine.frontend.business.device.index'),
      data: {
        page,
        device_no: this.data.keyword
      },
      success: resdata => {
        let data = resdata.data;
        if (data.result) {
          if(page == 1){
            this.setData({list:data.data.data});
          }else{
            let list = this.data.list.concat(data.data.data);
            this.setData({list})
          }
          this.data.current_page = data.data.current_page;
          this.data.last_page = data.data.last_page;
        } else {
          app.tips(data.msg)
        }
      }
    })
  },
  search() {
    this.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.current_page>=this.data.last_page){
      return;
    }
    let page = this.data.current_page + 1;
    this.getData(page)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})